﻿@using NewLife;
@using NewLife.Web;
@using XCode;
@using XCode.Configuration;
@{
    var fact = ViewBag.Factory as IEntityFactory;
    var page = ViewBag.Page as Pager;
    var fields = ViewBag.Fields as FieldCollection;
    var ukey = fact.Unique;
    var set = ViewBag.PageSetting as PageSetting ?? PageSetting.Global;
}
<table class="table table-bordered table-hover">
    <thead>
        <tr>
            @if (set.EnableSelect && ukey != null)
            {
                <th class="text-center" style="width:10px;"><input type="checkbox" id="chkAll" title="全选" /></th>
            }
            @foreach (var item in fields)
            {
                var sortUrl = !item.MapField.IsNullOrEmpty() ? page.GetSortUrl(item.MapField) : page.GetSortUrl(item.Name);
                var df = item as ListField;
                var name = df?.Header ?? item.DisplayName;
                var title = df?.HeaderTitle ?? item.Description;

                if (item.Type == typeof(DateTime) && item.Services.Count == 0)
                {
                    var width = item.ItemType == "Date" ? 80 : 134;
                    <th class="text-center" style="min-width:@(width)px;" title="@title"><a href="@Html.Raw(sortUrl)">@name</a></th>
                }
                else if (item.Type == null)
                {
                    <th class="text-center" title="@title">@name</th>
                }
                else
                {
                    <th class="text-center" title="@title"><a href="@Html.Raw(sortUrl)">@name</a></th>
                }
            }
            @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
            {
                <th class="text-center">操作</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var entity in Model)
        {
            <tr class="@(fields.GetRowClass?.Invoke(entity))">
                @if (set.EnableSelect && ukey != null)
                {
                    <td class="text-center"><input type="checkbox" name="keys" value="@entity[ukey.Name]" /></td>
                }
                @foreach (var item in fields)
                {
                    var df = item as ListField;
                    var txt = "";
                    if (df != null && df.DataVisible != null && !df.DataVisible(entity))
                    {
                        <td></td>
                    }
                    else if (df != null && (txt = df.GetLink(entity, page)) != null)
                    {
                        var tdClass = df.GetTextClass(entity);
                        <td class="@(tdClass??"text-center")">@Html.Raw(txt)</td>
                    }
                    else if (df != null && (txt = df.GetText(entity, page)) != null)
                    {
                        var tdClass = df.GetTextClass(entity);
                        <td class="@tdClass)">@Html.Raw(txt)</td>
                    }
                    else
                    {
                        @await Html.PartialAsync("_List_Data_Item", new ValueTuple<IEntity, DataField>(entity, item))
                    }
                }
                @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
                {
                    <td class="text-center">
                        @await Html.PartialAsync("_List_Data_Action", (Object)entity)
                    </td>
                }
            </tr>
        }
        @if (page.State is IEntity entity2)
        {
            @await Html.PartialAsync("_List_Data_Stat", entity2)
        }
    </tbody>
</table>